//
//  RecommendViewController.m
//  cm
//
//  Created by suyibaishan on 2017/11/4.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "RecommendViewController.h"
#import "RecommendCell.h"

@interface RecommendViewController () <UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
    NSMutableArray *dataSource;
    UILabel *menberLabel;
    UILabel *infoLabel;
    UIButton *invertedBtn;
    BOOL isIndirect;
}
@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloadData];
    
    [self createRecommendView];
}

-(void)reloadData
{
    [[ToolNet sharedManager] getUserInfo:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] url:isIndirect?MIndirectRecommend:MRecommend completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            [dataSource removeAllObjects];
            [dataSource addObjectsFromArray:responseJson[@"data"][@"list"]];
            [_tableView reloadData];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
    }];
}

-(void)createRecommendView
{
    self.titleLabel.text = @"直荐非直荐";
    self.backButton.hidden = NO;
    
    infoLabel = [[UILabel alloc] init];
    infoLabel.textAlignment = NSTextAlignmentCenter;
    infoLabel.font = [UIFont systemFontOfSize:35];
    infoLabel.text = @"— 暂无成员 —";
    infoLabel.textColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1];
    infoLabel.hidden = YES;
    [self.view addSubview:infoLabel];
    infoLabel.sd_layout
    .topSpaceToView(self.view, kScreenWidth*0.45f)
    .leftSpaceToView(self.view, 30)
    .widthIs(kScreenWidth - 60)
    .heightIs(40);
    
    menberLabel = [[UILabel alloc] init];
    menberLabel.layer.cornerRadius = 12.5;
    menberLabel.clipsToBounds = YES;
    menberLabel.textAlignment = NSTextAlignmentCenter;
    menberLabel.font = [UIFont systemFontOfSize:14];
    menberLabel.text = @"• 直荐成员   ";
    menberLabel.textColor = [UIColor whiteColor];
    menberLabel.backgroundColor = [UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1];
    [self.view addSubview:menberLabel];
    menberLabel.sd_layout
    .topSpaceToView(self.baseBar, 15)
    .leftSpaceToView(self.view, 15)
    .widthIs(70)
    .heightIs(25);
    [menberLabel setSingleLineAutoResizeWithMaxWidth:115];
    
    invertedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [invertedBtn setTitle:@"切换成员列表" forState:UIControlStateNormal];
    [invertedBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    invertedBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    UIImage *invertedImg = [[UIImage imageNamed:@"system_mini"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [invertedBtn setImage:invertedImg forState:UIControlStateNormal];
    [invertedBtn addTarget:self action:@selector(invertedBtnHanlder) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:invertedBtn];
    invertedBtn.sd_layout
    .topSpaceToView(self.baseBar, 25)
    .rightSpaceToView(self.view, 10)
    .widthIs(100)
    .heightIs(35);
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.baseBarHeight + 55, kScreenWidth, kScreenHeight - self.baseBarHeight - 65) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [self.view addSubview:line];
    line.sd_layout
    .leftSpaceToView(self.view, 10)
    .topSpaceToView(self.baseBar, 55)
    .widthIs(kScreenWidth - 20)
    .heightIs(0.5);
    
    dataSource = [NSMutableArray array];
}

-(void)invertedBtnHanlder
{
    if (isIndirect) {
        menberLabel.text = @"• 直荐成员   ";
        isIndirect = NO;
    }
    else {
        menberLabel.text = @"• 非直荐成员   ";
        isIndirect = YES;
    }
    [self reloadData];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";
    RecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[RecommendCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.titleLabel.text = isIndirect?dataSource[indexPath.row][@"acceptUserName"]:dataSource[indexPath.row][@"realName"];
    cell.timeLabel.text = dataSource[indexPath.row][@"createTime"];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
